package com.youlai.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.youlai.system.model.entity.AttendanceSetting;
import com.youlai.system.mapper.AttendanceSettingMapper;
import com.youlai.system.model.form.AttendanceSettingForm;
import com.youlai.system.service.IAttendanceSettingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Data;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 只茂瑞
 * @since 2024-05-30
 */
@Service
@Data
public class AttendanceSettingServiceImpl extends ServiceImpl<AttendanceSettingMapper, AttendanceSetting> implements IAttendanceSettingService {

    private final AttendanceSettingMapper attendanceSettingMapper;
    @Override
    public AttendanceSettingForm getAttendanceSettingForm(Long id) {
        AttendanceSetting attendanceSetting = this.getById(id);
        AttendanceSettingForm attendanceSettingForm = new AttendanceSettingForm();
        BeanUtil.copyProperties(attendanceSetting, attendanceSettingForm);
        attendanceSettingForm.setAfternoonEndTime(attendanceSettingForm.getAfternoonEndTime());
        return attendanceSettingForm;
    }

    @Override
    public Integer saveOrUpdate(AttendanceSettingForm attendanceSettingForm) {
        System.out.println(attendanceSettingForm);
        Integer i = attendanceSettingMapper.countByDepartmentId(attendanceSettingForm.getDepartmentId());
        AttendanceSetting attendanceSetting = new AttendanceSetting();
        BeanUtil.copyProperties(attendanceSettingForm, attendanceSetting);

        if (i > 0){
//            UpdateWrapper<AttendanceSetting> updateWrapper = new UpdateWrapper<AttendanceSetting>()
//                    .eq("department_id",attendanceSettingForm.getDepartmentId())
//                    .set("morning_start_time",attendanceSettingForm.getMorningStartTime())
//                    .set("morning_end_time",attendanceSettingForm.getMorningEndTime())
//                    .set("afternoon_start_time",attendanceSettingForm.getAfternoonStartTime())
//                    .set("afternoon_end_time",attendanceSettingForm.getAfternoonEndTime())
//                    .set("remarks",attendanceSettingForm.getRemarks());
            boolean update = attendanceSettingMapper.updateAttendanceSetting(attendanceSetting);
            Assert.isTrue(update,"考勤设置修改失败");
        }else {
            boolean save = this.saveOrUpdate(attendanceSetting);
            Assert.isTrue(save,"考勤设置新增失败");
        }

        return attendanceSetting.getId();
    }
}
